 -module(agent_httpd_register).  
 -export([start/0, loop/1]).  
   
-include("agent.hrl").

 start() ->  
     gen_socket_server:start(agent_httpd_register, 9000 , {?MODULE, loop}).  
   

 loop(S) ->  
          inet:setopts(S,[{active,once}]),
          receive
               {tcp,S,Data} ->
                   case  io_lib:fread("port=~d\r\n",binary_to_list(Data) ) of
                    {ok , [Port],_} -> 
                             error_logger:info_msg("recving a httpd regist  msg , port=~p~n",[Port]),
                             mnesia_op:insert(#httpd_proc{port=Port,desc=""}),
                             gen_tcp:send(S,Data),
                             loop(S);
        	    {more, _RestFormat, _Nchars, _InputStack} ->
	                     loop(S);			
	  	    {error, What}  ->
	                	error_logger:info_msg(io_lib:format("reponse format error: What=~p,Line=~p~n",
	                	                                [What,binary_to_list(Data)])),
       	                     loop(S)
		   end;
               {tcp_closed,S} ->
                   error_logger:info_msg("httpd close the connect ~n"),
                   ok
           end.

